<script lang="ts" setup>
import { get, post, message, loadFile } from '@/utils'
import { FormInstance } from '@arco-design/web-vue'
import Editor from '@/components/Editor/index.vue'
const props = defineProps<{
  show: boolean
  id: number | string
}>()
const emit = defineEmits(['update:show', 'reset', 'confirm'])
const formRef = ref() as Ref<FormInstance>
const detail = reactive({
  id: props.id || '',
  detail: '',
  icon: '',
  name: '',
  sort: 0,
  status: 0,
})
const submit = done => {
  formRef.value.validate(err => {
    if (!err) {
      const query = Object.assign({}, detail)
      post(
        { url: '/server-shop/admin/goods/serve/submit', data: query },
        ({ code, msg }) => {
          message({
            type: code == 200 ? 'success' : 'error',
            content: msg,
          })
          if (code == 200) {
            emit('update:show', false)
            emit('confirm')
          }
          done(code == 200)
        },
        () => done(false)
      )
    } else done(false)
  })
}
const getInfo = () => {
  get({ url: '/server-shop/admin/goods/serve/info', data: { id: props.id } }, ({ data }) => {
    Object.assign(detail, data)
  })
}
onMounted(() => {
  if (props.id) getInfo()
})
</script>
<template>
  <CModal v-model:visible="props.show" @before-ok="submit" width="700px" :mask-closable="false" :title="props.id ? '编辑' : '新建'" @cancel="emit('update:show', false)">
    <div class="content">
      <a-form :model="detail" ref="formRef" :label-col-props="{ span: 4 }" :wrapper-col-props="{ span: 20 }">
        <!-- <a-form-item label="图标" field="icon" :rules="[{ required: true, message: '请上传图标' }]" :validate-trigger="['change', 'blur']">
          <Upload :list="detail.icon" v-model:value="detail.icon" />
        </a-form-item> -->
        <a-form-item label="状态" field="status" :rules="[{ required: true, message: '请输入排序值' }]" :validate-trigger="['change', 'blur']">
          <a-radio-group
            type="button"
            v-model="detail.status"
            :options="[
              { label: '显示', value: 1 },
              { label: '隐藏', value: 0 },
            ]"
          />
        </a-form-item>
        <a-form-item label="服务名称" field="name" :rules="[{ required: true, message: '请输入服务名称' }]" :validate-trigger="['change', 'blur']">
          <a-input v-model="detail.name" placeholder="请输入服务名称" />
        </a-form-item>
        <a-form-item label="排序值" field="sort" :rules="[{ required: true, message: '请输入排序值' }]" :validate-trigger="['change', 'blur']">
          <a-input-number :min="0" v-model="detail.sort" placeholder="请输入排序值" />
        </a-form-item>
        <a-form-item label="内容" field="detail" :rules="[{ required: true, message: '请输入内容' }]" :validate-trigger="['change', 'blur']">
          <Editor v-model:content="detail.detail" />
        </a-form-item>
      </a-form>
    </div>
  </CModal>
</template>
<style lang="less" scoped></style>
